package com.xinlong.shop.api.temp;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import com.xinlong.shop.framework.common.R;
import com.xinlong.shop.core.temp.entity.BuyStatistics;
import com.xinlong.shop.core.temp.service.IBuyStatisticsService;
import javax.validation.Valid;

/**
 * <p>
 * 访问统计-临时给老系统用的 前端控制器
 * </p>
 *
 * @author Sylow
 * @since 2022-12-23
 */
@RestController
@RequestMapping("/temp/buyStatistics")
public class BuyStatisticsController {

    private final IBuyStatisticsService buyStatisticsService;

    public BuyStatisticsController(IBuyStatisticsService buyStatisticsService) {
        this.buyStatisticsService = buyStatisticsService;
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public R page(Integer current, Integer size){
        Page<BuyStatistics> page = new Page<>(current, size);
        page = this.buyStatisticsService.page(page);
        return R.success("操作成功", page);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public R save(@Valid @RequestBody BuyStatistics buyStatistics){

        int hour = DateUtil.thisHour(true);
        int minute = DateUtil.thisMinute();
        if (buyStatistics.getPageId().intValue() == 1) {
            // 10点11点以外的数据不统计
            if (hour == 10 && minute < 33) {
                Long date = DateUtil.current();
                buyStatistics.setVisitTime(date);
                this.buyStatisticsService.save(buyStatistics);
                return R.success("操作成功");
            }
        } else {
            if (hour > 10 && hour < 15) {
                Long date = DateUtil.current();
                buyStatistics.setVisitTime(date);
                this.buyStatisticsService.save(buyStatistics);
                return R.success("操作成功");
            }
        }
        return R.success("不记录");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public R update(@Valid @RequestBody BuyStatistics buyStatistics, @PathVariable("id") Integer id){
        this.buyStatisticsService.update(buyStatistics, id);
        return R.success("操作成功", buyStatistics);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public R delete(@PathVariable("id") Integer id) {
        this.buyStatisticsService.delete(id);
        return R.success("删除成功");
    }

    @RequestMapping(value = "/{rule_id}", method = RequestMethod.GET)
    public R visitNum(@PathVariable("rule_id") String ruleId, Integer pageId){
        if (pageId == null) {
            pageId = 0;
        }
        Integer num = this.buyStatisticsService.visitNum(ruleId, pageId);
        return R.success("操作成功", num);
    }



    public static void main(String[] args) {
        Long date = DateUtil.current();
        int hour = DateUtil.thisHour(true);
        System.out.println(date);
        System.out.println(hour);
    }

}
